package fr.umlv.history;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;

import kiyut.ekspos.BrowserWindow;

import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.ObjectId;
import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
import org.jdesktop.swingx.JXErrorPane;
import org.jdesktop.swingx.error.ErrorInfo;

import fr.umlv.authentication.SessionHandler;
import fr.umlv.cmis.gedconfig.Shortcut;
import fr.umlv.cmis.gedconfig.XMLAccess;
import fr.umlv.model.ClientModel;

// TODO: Auto-generated Javadoc
/**
 * The Class HistoryView.
 */
@SuppressWarnings("serial")
public class HistoryView extends JPanel {

	/** The bundle. */
	private ResourceBundle bundle = ResourceBundle.getBundle("kiyut.ekspos.BrowserWindow", SessionHandler.getLocale());
	
	/** The t history. */
	private final JTable tHistory;
	
	/** The map history. */
	private HashMap<String, Shortcut> mapHistory;
	
	/** The model history. */
	private final DataHistoryTableModel modelHistory;
	
	/** The instance. */
	private static HistoryView instance = null;
	
	/**
	 * Gets the single instance of HistoryView.
	 *
	 * @return single instance of HistoryView
	 */
	public static HistoryView getInstance() {
		if(instance==null)
			instance = new HistoryView();
		return instance;
	}
	
	/**
	 * Instantiates a new history view.
	 */
	private HistoryView() {

		setLayout(new GridLayout());
		
		mapHistory = XMLAccess.getInstance().getHistory();

		List<String> keys = new ArrayList<String>();
		for (Map.Entry<String, Shortcut> e : mapHistory.entrySet()) {
			keys.add(e.getKey());
		}

		modelHistory = new DataHistoryTableModel(mapHistory.size(), 2, keys);
		tHistory = new JTable(modelHistory);
		tHistory.setDefaultRenderer(JButton.class, new DefaultTableCellRenderer() {
			@Override
			public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {

				JButton actualButton = (JButton) value;
				
				actualButton.setForeground(Color.black);
				actualButton.setFocusPainted(false);
				actualButton.setBorderPainted(false);
				actualButton.setContentAreaFilled(false);
				
				if (col == 0) {
					table.getColumnModel().getColumn(col).setPreferredWidth(140);
					actualButton.setHorizontalAlignment(SwingConstants.LEFT);
				}
				
				if (col == 1) {
					table.getColumnModel().getColumn(col).setMaxWidth(20);
				}
				
				

				return actualButton;
			}
		});

		add(tHistory);
	}

	/**
	 * Inits the.
	 */
	public void init() {

		tHistory.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {

				TableColumnModel columnModel = tHistory.getColumnModel();
				int column = columnModel.getColumnIndexAtX(e.getX());
				int row = e.getY() / tHistory.getRowHeight();
				Object value;

				if (row >= tHistory.getRowCount() || row < 0 || column >= tHistory.getColumnCount() || column < 0)
					return;

				value = tHistory.getValueAt(row, column);

				CmisObject cmisDoc = null;

				try {
					String key = ((JButton) tHistory.getValueAt(row, 0)).getText();
					ObjectId objID = SessionHandler.getClientSession().getSession().createObjectId(mapHistory.get(key).getId());
					cmisDoc = SessionHandler.getClientSession().getSession().getObject(objID);
				} catch (CmisObjectNotFoundException e2) {
					JOptionPane.setDefaultLocale(SessionHandler.getLocale());
					JOptionPane.showMessageDialog(BrowserWindow.getInstance(), bundle.getString("OpenExist.text"), "Error", JOptionPane.ERROR_MESSAGE);
					return;
				}

				if ((value instanceof JButton)) {
					if (column == 1) {
						if (BaseTypeId.CMIS_DOCUMENT.equals(cmisDoc.getBaseTypeId())) {
							BrowserWindow.getInstance().updateLocationModel(((Document) cmisDoc).getParents().get(0));
						} else
							BrowserWindow.getInstance().updateLocationModel(cmisDoc);
					} else if (column == 0) {
						try {
							ClientModel.openCmisObject(cmisDoc);
						} catch (IOException e1) {
							JXErrorPane.showDialog(BrowserWindow.getInstance(), new ErrorInfo(e1.getMessage(), e1.getMessage(), null, "IOException", e1, Level.WARNING, null));
						}
					}
				} else {
				}
			}
		});

	}
	
	/**
	 * Update.
	 */
	public void update() {
		mapHistory = XMLAccess.getInstance().getHistory();
		modelHistory.update();
		tHistory.revalidate();
	}

}
